package com.company;

public class Main {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int val){
            this.val = val;
        }
    }

    public static void main(String[] args) {
	// write your code here
        ListNode nodeSta = new ListNode(1);
        ListNode nextNode;
        nextNode=nodeSta;

        //创建链表
        for(int i=1; i <= 3; i++){
            for(int j = 1; j <= 2; j++){
                ListNode node = new ListNode(i);
                nextNode.next = node;
                nextNode=nextNode.next;
            }
        }
        nextNode=nodeSta;
        print(nextNode);
        System.out.println("删除重复元素后:");
        print(deleteDuplicates(nextNode));
    }


    public static void print(ListNode n){
        while(n != null){
            System.out.println("节点:" + n.val);
            n = n.next;
        }
        System.out.println();
    }

    public static ListNode deleteDuplicates(ListNode head) {
        ListNode ret = head;
        while(ret != null && ret.next != null){
            if(ret.val == ret.next.val){
                ret.next = ret.next.next;
            } else {
                ret = ret.next;
            }
        }
        return head;
    }

}
